package com.shujia.day16.tcp.tcp2;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        /*
            TODO
                1.可以一直接收客户端的数据 当有数据发送过来时，就打印出来

         */
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("启动服务端...");
        Socket accept = serverSocket.accept();
        System.out.println("接收客户端连接");
        InetAddress inetAddress = accept.getInetAddress();
        // TODO 获取输入流
        InputStream inputStream = accept.getInputStream();
        // TODO 获取输出流
        OutputStream outputStream = accept.getOutputStream();


        // 包装数据成字符流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String readLine = "";
        while ((readLine = bufferedReader.readLine()) != null) {
            System.out.println(inetAddress.getHostAddress() + ":" + readLine);
            outputStream.write("服务端接收到消息\n".getBytes());
        }
        bufferedReader.close();
        inputStream.close();
        accept.close();
    }
}
